﻿package mortal.game.view.copy.defenseCopy
{
    import Message.DB.Tables.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.copy.defenseCopy.view.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.interfaces.*;

    public class DefenseCopyShop extends BaseWindow
    {
        private var _tabData:Array;
        private var _tabBar:GTabarNew;
        private var _list:GTileList;
        private var _pageSelecter:PageSelecter;
        private static var _instance:DefenseCopyShop;

        public function DefenseCopyShop(param1:ILayer = null)
        {
            this._tabData = Language.getArray(50151);
            super(param1);
            setSize(320, 370);
            title = "" + Language.getString(70870);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(10, 60, 302, 275, this));
            this._tabBar = UIFactory.gTabBarNew(4, 30, this._tabData, 312, 378, 65, 24, this, this.tabBarChangeHandler, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this._list = UIFactory.tileList(16, 66, 290, 273, this);
            this._list.rowHeight = 52;
            this._list.columnWidth = 146;
            this._list.setStyle("cellRenderer", DefenseShopCellRenderer);
            this._list.rowCount = 5;
            this._list.columnCount = 2;
            this._pageSelecter = UIFactory.pageSelecter(97, 341, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, new GTextFormat());
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.pageTextBoxSize = 36;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this.updateAll();
            return;
        }// end function

        public function updateAll() : void
        {
            this.updateList();
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this._pageSelecter.currentPage = 1;
            this.updateList();
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.updateList();
            return;
        }// end function

        private function sortFun(param1:TDefenseCopyShopSell, param2:TDefenseCopyShopSell) : int
        {
            var _loc_3:* = new ItemData(param1.itemCode);
            var _loc_4:* = new ItemData(param2.itemCode);
            if (_loc_3.itemInfo.color < _loc_4.itemInfo.color)
            {
                return -1;
            }
            if (_loc_3.itemInfo.color > _loc_4.itemInfo.color)
            {
                return 1;
            }
            if (_loc_3.itemCode < _loc_4.itemCode)
            {
                return -1;
            }
            if (_loc_3.itemCode > _loc_4.itemCode)
            {
                return 1;
            }
            return 0;
        }// end function

        public function updateList() : void
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = ArrayUtil.DictAsArr(DefenseCopyShopConfig.instance.getConfigBySellType((this._tabBar.selectedIndex + 1)));
            _loc_2.sort(this.sortFun);
            var _loc_3:* = this._list.rowCount * this._list.columnCount;
            var _loc_4:* = (this._pageSelecter.currentPage - 1) * _loc_3;
            var _loc_5:* = Math.min(_loc_3 + _loc_4, _loc_2.length);
            var _loc_6:* = _loc_4;
            while (_loc_6 < _loc_5)
            {
                
                _loc_1.addItem(_loc_2[_loc_6]);
                _loc_6++;
            }
            this._list.dataProvider = _loc_1;
            this._list.drawNow();
            this._pageSelecter.maxPage = Math.ceil(_loc_2.length / _loc_3);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._tabBar.dispose(param1);
            this._list.dispose(param1);
            this._pageSelecter.dispose(param1);
            this._tabBar = null;
            this._list = null;
            this._pageSelecter = null;
            return;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function get instance() : DefenseCopyShop
        {
            if (_instance == null)
            {
                _instance = new DefenseCopyShop;
            }
            return _instance;
        }// end function

    }
}
